package org.example.dao;

import org.example.model.ImageInfo;
import org.example.util.DBUtil;

import java.awt.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ImageDao {

    public static int insert(ImageInfo image) {
        Connection c = null;
        PreparedStatement ps = null;
        try {
            c = DBUtil.getConnection();
            String sql = "insert into image_info values(null, ?, ?,?,?,?,?)";
            ps = c.prepareStatement(sql);
            ps.setString(1, image.getImageName());
            ps.setLong(2,image.getSize());
            long time = image.getUploadTime().getTime();
            ps.setTimestamp(3,new Timestamp(time));
            ps.setString(4, image.getMd5());
            ps.setString(5,image.getContentType());
            ps.setString(6,image.getPath());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException("数据库保存图片失败",e);
        }finally {
            DBUtil.close(c,ps,null);
        }
    }

    public static List<ImageInfo> selectAll() {
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = DBUtil.getConnection();
            String sql = "select * from image_info";
            ps = c.prepareStatement(sql);
            // 查询返回结果集
            rs = ps.executeQuery();
            // 返回的list
            List<ImageInfo> images = new ArrayList<>();
            while (rs.next()) {
                ImageInfo imageInfo = new ImageInfo();
                imageInfo.setImageId(rs.getInt("image_id"));
                imageInfo.setImageName(rs.getString("image_name"));
                images.add(imageInfo);
            }
            return images;
        } catch (SQLException e) {
            throw new RuntimeException("获取图片列表出错",e);
        }finally {
            DBUtil.close(c,ps,rs);
        }
    }

    // 根据id查询
    public static ImageInfo selectOne(int id) {
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = DBUtil.getConnection();
            String sql = "select * from image_info where image_id=?";
            ps = c.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            // 查询返回结果集
            rs = ps.executeQuery();
            while (rs.next()) {
                ImageInfo imageInfo = new ImageInfo();
                imageInfo.setImageId(rs.getInt("image_id"));
                imageInfo.setImageName(rs.getString("image_name"));
                imageInfo.setPath(rs.getString("Path"));
                return imageInfo;
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException("获取图片列表出错",e);
        }finally {
            DBUtil.close(c,ps,rs);
        }
    }

    public static int delete(Integer id) {
        Connection c =null;
        PreparedStatement ps = null;
        try {
            c = DBUtil.getConnection();
            String sql = "delete from image_info where image_id=?";
            ps = c.prepareStatement(sql);
            ps.setInt(1,id);
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException("数据库删除图片失败",e);
        }

    }

    public static ImageInfo selectByMd5(String md5) {
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = DBUtil.getConnection();
            String sql = "select * from image_info where md5=?";
            ps = c.prepareStatement(sql);
            ps.setString(1,md5);
            rs = ps.executeQuery();
            // 查询返回结果集
            rs = ps.executeQuery();
            while (rs.next()) {
                ImageInfo imageInfo = new ImageInfo();
                imageInfo.setImageId(rs.getInt("image_id"));
                imageInfo.setImageName(rs.getString("image_name"));
                imageInfo.setPath(rs.getString("Path"));
                return imageInfo;
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException("根据MD5查询出错",e);
        }finally {
            DBUtil.close(c,ps,rs);
        }
    }
}
